/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.business.boundary;

import java.security.Principal;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import meteocal.business.security.entity.User;
import sun.security.krb5.internal.ktab.KeyTabConstants;

/**
 *
 * @author luana
 */
@Stateless
public class UserFacade extends AbstractFacade<User> {
    @PersistenceContext(unitName = "meteocalPU")
    EntityManager em;

    private String username;
    
    @Inject
    Principal principal;

    public String getUsername() {
        return principal.getName();
    }   
    
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }
    
      
    /**
     * 
     * Before processing the queries, the parameters are adjusted with the wildcards '%' in order to have partial
     * searches too.
     * @param username
     * @return 
     */
    
    public List<User> searchUserUsername(String username){
     
        username = "%"+username+"%";
        return em.createNamedQuery(User.FIND_USER_BY_USERNAME, User.class).setParameter("username",username).getResultList();
       
    }
    
      
    public List<User> getAllUser(){
        return em.createNamedQuery(User.FIND_ALL, User.class).getResultList();
    }
    
   
    public List<User> searchUserName(String name){
        name = "%"+name+"%";
        return em.createNamedQuery(User.FIND_USER_BY_NAME, User.class).setParameter("name",name).getResultList();
    }
    
    public List<User> searchUserCity(String city){
        city = "%"+city+"%";
        return em.createNamedQuery(User.FIND_USER_BY_CITY, User.class).setParameter("city",city).getResultList();
    } 
    
    
    
}
